<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use think\facade\Env;
use think\helper\Str;
use app\model\Bazaar;
use app\model\Zone;
use OSS\OssClient;
use OSS\Core\OssException;

class ServiceCount extends Common
{

	/**
	 * 查询数据
	 *
	 * @return
	 */
	public function read(Request $request) {
		if($this->hasPermission($request->sessInfo, 'servicecount:read:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );

  	$platform = $request->param('platform', '', 'trim');
		if($platform) {
			array_push( $where, ['platform', '=',  $platform ] );
		}

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if($bazaarId) {
			array_push( $where, ['bazaarId', '=',  $bazaarId ] );
		}

		$zoneid = $request->param('zoneid', '', 'trim');
		if($zoneid) {
			array_push( $where, ['zoneid', '=',  $zoneid ] );
		}

		$nickname = $request->param('nickname', '', 'trim');
		if($nickname) {
			array_push( $where, ['nickname', 'like',  "%{$nickname}%" ] );
		}

		$wechat = $request->param('wechat', '', 'trim');
		if($wechat) {
			array_push( $where, ['wechat', 'like',  "%{$wechat}%" ] );
		}

		$name = $request->param('name', '', 'trim');
		if($name) {
			array_push( $where, ['name', 'like',  "%{$name}%" ] );
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['datetime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, false) ] );
		} else {
			array_push( $where, ['datetime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00', strtotime("-1 day")), date('Y-m-d 00:00:00'), false) ] );
		}

		$query = Db::name('cfg_service_analyse')->field('id,platform,bazaarId,zoneid,name,nickname,wechat,shownumber,copynumber,cost, 
		IF(ROUND(cost / shownumber, 2 ), ROUND(cost / shownumber, 2 ), `cost`) as `cpcshow`,
		IF(ROUND(cost / copynumber, 2 ), ROUND(cost / copynumber, 2 ), `cost`) as `cpccopy`	')
			->where($where)->order($sort, $order)->group('wechat')->paginate($limit);
		
		$total = Db::table('cfg_service_analyse')
		->field('sum(shownumber) as shownumber,
			sum(copynumber) as copynumber,
			sum(cost) as cost,
			IF(ROUND( sum(`cost`) / sum(`shownumber`), 2 ), ROUND( sum(`cost`) / sum(`shownumber`), 2 ), sum(`cost`)) as `cpcshow`,
			IF(ROUND( sum(`cost`) / sum(`copynumber`), 2 ), ROUND( sum(`cost`) / sum(`copynumber`), 2 ), sum(`cost`)) as `cpccopy`
			')
		->where($where)
		->find();

		$total['shownumber'] = $total['shownumber'] ? $total['shownumber'] : 0;
		$total['copynumber'] = $total['copynumber'] ? $total['copynumber'] : 0;
		$total['cpccopy'] = $total['cpccopy'] ? $total['cpccopy'] : 0;
		$total['cpcshow'] = $total['cpcshow'] ? $total['cpcshow'] : 0;
		$total['cost'] = $total['cost'] ? $total['cost'] : 0;

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items(), 'total' => $total,
			'bazaar' => Bazaar::getList($request->sessInfo, false),
			'zone' => Zone::getList($request->sessInfo)
		];
	}

}
